package com.topInterview;

import com.sun.xml.internal.org.jvnet.mimepull.CleanUpExecutorFactory;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/20 21:42
 */
public class Leecode108_BuildBST {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return process(nums, 0, nums.length - 1);
    }

    public TreeNode process(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        if (left == right) {
            return new TreeNode(nums[left]);
        }

        int mid = (left + right) >> 1;
        TreeNode head = new TreeNode(nums[mid]);
        head.left = process(nums, left, mid - 1);
        head.right = process(nums, mid + 1, right);

        return head;
    }
}
